package org.eclipseuseradmin.ui.editors;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipseuseradmin.internal.ui.editors.ChangePasswordDialog;
import org.eclipseuseradmin.internal.ui.editors.EditorsMessages;
import org.eclipseuseradmin.ui.model.UserModel;

public class UserGeneralInfoSection extends RoleElementGeneralInfoSection {

	protected Hyperlink changePasswordLink;

	public UserGeneralInfoSection(Composite parent, UserOverviewPage page) {
		super(parent, page, EditorsMessages.get().UserGeneralInfoSection_title,
				EditorsMessages.get().UserGeneralInfoSection_description,
				EditorsMessages.get().UserGeneralInfoSection_tooltip, 0);
	}

	@Override
	protected void createClient(Section section, FormToolkit toolkit) {

		super.createClient(section, toolkit);

		changePasswordLink = toolkit.createHyperlink(client, EditorsMessages
				.get().UserGeneralInfoSection_changePassword, SWT.WRAP);
		TableWrapData layoutData = new TableWrapData();
		layoutData.colspan = 2;
		changePasswordLink.setLayoutData(layoutData);
		changePasswordLink.addHyperlinkListener(new HyperlinkAdapter() {
			@Override
			public void linkActivated(HyperlinkEvent event) {

				handelChangePassword();
			}
		});
	}

	protected void handelChangePassword() {

		UserModel model = (UserModel) getModel();

		ChangePasswordDialog dialog = new ChangePasswordDialog(getSection()
				.getShell());
		if (dialog.open() == Dialog.OK) {
			model.changePassword(dialog.getPassword());
		}
	}

}
